package com.zwz.controller;

import com.zwz.crawler.basic.TungeeEnterpriseInfo;
import com.zwz.crawler.basic.TungeeEnterpriseInfo_weburl;
import com.zwz.crawler.mangeInfo.weburl.StrictTungeeRequest;
import com.zwz.entity.EnterpriseInfo;
import com.zwz.entity.OfficialWebsiteInfo;
import com.zwz.service.OfficialWebsiteInfoService;
import com.zwz.vo.WebUrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/websites")
public class OfficialWebsiteInfoController {

    @Autowired
    private OfficialWebsiteInfoService service;

    @PostMapping
//    public ResponseEntity<?> create(@RequestBody OfficialWebsiteInfo record) {
    public ResponseEntity<?> create( String enterpriseId) {
        try {

            OfficialWebsiteInfo info= TungeeEnterpriseInfo_weburl.startTungeeEnterpriseInfo(enterpriseId);

            WebUrl bean = StrictTungeeRequest.StartStrictTungeeRequest(enterpriseId);
            info.setIpPhysicalLocation(bean.getLcoation());
            info.setIcpRecordDate(bean.getTime());
            info.setIpOperationNode(bean.getNode());
            int key=0;
            info.setId(key++);
            service.create(info);
            return ResponseEntity.status(HttpStatus.CREATED).body(info);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public ResponseEntity<OfficialWebsiteInfo> getById(@PathVariable Integer id) {
        OfficialWebsiteInfo info = service.getById(id);
        return info != null ? ResponseEntity.ok(info) : ResponseEntity.notFound().build();
    }

    @GetMapping("/search")
    public ResponseEntity<OfficialWebsiteInfo> getByUrl(
            @RequestParam("url") String url) {
        OfficialWebsiteInfo info = service.getByUrl(url);
        return info != null ? ResponseEntity.ok(info) : ResponseEntity.notFound().build();
    }

    @GetMapping("/icp-status")
    public ResponseEntity<List<OfficialWebsiteInfo>> getByIcpStatus(
            @RequestParam Boolean hasIcp) {
        return ResponseEntity.ok(service.getByIcpStatus(hasIcp));
    }

    @PutMapping("/{id}")
    public ResponseEntity<?> update(
            @PathVariable Integer id,
            @RequestBody OfficialWebsiteInfo record) {
        try {
            if (!id.equals(record.getId())) {
                return ResponseEntity.badRequest().body("ID不一致");
            }
            service.update(record);
            return ResponseEntity.ok(record);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> delete(@PathVariable Integer id) {
        service.delete(id);
        return ResponseEntity.noContent().build();
    }
}